local BT = require('bt')
local tree = require('bt_input')
local Rule = require('Rule')
local next1 = require('bt_next1')
local dump = function(...) print(dump(...)) end 

local Ai = class("Ai")
	-- self.card = {
	-- 	31,32,33,34,71,81,91,101,111,121,131,
	-- 	41,42,43,44,72,82,92,102,112,122,132,
	-- 	51,52,53,54,73,83,93,103,113,123,133,
	-- 	61,62,63,64,74,84,94,104,114,124,144,154,
	-- }
function Ai:ctor()
	self.rule = Rule.new()
	self.myCards = {
			31,32,33,71,81,91,101,64,131,133,41,42,43,72,54,
		}--{31,32,33,34, 71,81,91,101,111,121,131, 41,42,43,44,}
	self.rule:sortCards(self.myCards)
	self.tbCard = nil
	self.otherCards1 = {
				[1]  = 123,
				[2]  = 54,
				[3]  = 52,
				}-- {72,82,92,102,112,122,132,51,52,53,54,73,83,93,103,} --别的玩家的牌
	self.otherCards2 = {113,123,133,61,62,63,64,74,84,94,104,114,124,144,154,}
	self.rule:sortCards(self.myCards)
	self.rule:sortCards(self.otherCards1)
	self.tbPutCard = {} --候选出牌列表
	self.seatid = 1 --自己的座位id
	self.nextSeatid = 2
	-- self.tbPutCard = self.rule:putHandCard(self.myCards)
	--
	self:getPutCard()

end

function Ai:setSeatid(seatid)
	self.seatid = seatid
	self.nextSeatid = seatid + 1
	if self.nextSeatid > 3 then 
		self.nextSeatid = 1
	end
end

--设置牌
function Ai:setCard(tbCard)
	self.myCards = tbCard[self.seatid]
	self.otherCards1 = tbCard[self.nextSeatid]
end

--取牌
function Ai:getPutCard(otherCards)
	print("_________otherCards___",otherCards)
	if not otherCards or not next(otherCards) then 
		self.tbPutCard = self.rule:putHandCard(self.myCards)
	else
		self.tbPutCard = self.rule:passivePut(self.myCards,otherCards)
	end
	-- dump(self.tbPutCard)
	if not next(self.tbPutCard) then 
		return nil
	end
	return self:checkNext1()
end

--单张小的先出
function Ai:danZhanSmall(putCard)
	local card = putCard.putCard
	-- print("___danZhanSmall____",dump(card))
	local tbDanZhang = {}
	if self.rule:getCardType(card)==self.rule.cardType.danZhang then 
		for k,v in pairs(self.tbPutCard) do 
			if self.rule:getCardType(v.putCard)==self.rule.cardType.danZhang then 
				table.insert(tbDanZhang,v)
			end
		end
		table.sort(tbDanZhang,function(a,b)
			if a.value > b.value then 
				return true
			elseif a.value == b.value then 
				return self.rule:getCardNum(a.putCard[1])<self.rule:getCardNum(b.putCard[1])
			end
		end)
		putCard = tbDanZhang[1]			
	end
	return putCard
end

--检查下家是不是单双牌
function Ai:checkNext1()
	local putCard = self.tbPutCard[1]
	-- dump(putCard)
	local func={
		nextIsDan=function ( ... )
			if 1==#self.otherCards1 then 
				return true
			end 
			return false
		end,
		nextIsDui=function ( ... )
			if 2==#self.otherCards1 then 				
				return true
			end 
			return false
		end,	
		nextIsSan=function ( ... )
			if 3==#self.otherCards1 then 		
				--1 3张中对子比我大，我不能对子
				local cards = putCard.putCard
				if #cards == 2 then 	
					local dz = self.rule:getDuiZi(self.otherCards1)	
					if dz and next(dz) then 
						dz = dz[1]
						if self.rule:getCardNum(cards[1]) < self.rule:getCardNum(dz[1]) then 
							local dz2 = self.rule:getDuiZi(putCard.restCard)						
							if dz2 and next(dz2) then 
								dz2 = dz2[1]
								if self.rule:getCardNum(dz2[1]) > self.rule:getCardNum(dz[1]) then									
									return false
								end
							end
							return true
						end
					end		
				end			
				return false
			end 
			return false
		end,		
		hasEr=function ( ... )
			for k,v in pairs(self.otherCards1) do 
				if self.rule:getCardNum(v) == 15 then 
					return true
				end
			end
			return false
		end,				
		other=function ( ... )
			return true
		end,	

		-----------------------Action

		nextIsDanAction=function ( ... )
			print("___nextIsDanAction__下家是单张_")
			local justHasDan = true
			for k,v in pairs(self.tbPutCard) do 
				if #v.putCard~=1 then 
					putCard = v
					justHasDan = false
					break
				end
			end
			if justHasDan then
				--只有单只大先出 
				table.sort(self.tbPutCard,function(a,b)
					return self.rule:getCardNum(a.putCard[1])>self.rule:getCardNum(b.putCard[1])
				end)
				putCard = self.tbPutCard[1]
			end
		end,
		nextIsDuiAction=function ( ... )
			print("___nextIsDuiAction_下家两张牌不出对子__")
			for k,v in pairs(self.tbPutCard) do 
				-- print("______",#v.putCard)
				if #v.putCard~=2 then 					
					putCard = v
					break
				end
			end			
			putCard = self:danZhanSmall(putCard)
		end,	
		nextIsSanAction=function ( ... )
			print("___nextIsSanAction_下家3张牌__")
			for k,v in pairs(self.tbPutCard) do 
				-- print("______",#v.putCard)
				if #v.putCard~=2 then 					
					putCard = v
					break
				end
			end			
			putCard = self:danZhanSmall(putCard)
		end,			
		hasErAction=function ( ... )
			print("__hasErAction__下家有2")
			putCard = self:danZhanSmall(putCard)
		end,		
		otherAction=function ( ... )
			print("__otherAction__其它情况")
			putCard = self:danZhanSmall(putCard)
		end,

	}
	BT.run(next1,func)
	-- dump(putCard)
	return putCard.putCard
end



local ai = Ai.new()

-- function main()

-- 	BT.run(tree, person)
-- end

-- main()

return Ai